package statistics

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/order"

	"github.com/gin-gonic/gin"
)

func Out(c *gin.Context) {

	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)

	//查询信息
	var orderStruct order.OrderStruct
	var orderData []order.OrderStruct
	model.GetModelData(
		model.Db,
		orderStruct.TableName(),
		&orderData,
		model.QueryOptions{
			SelectedFields: []string{"out_time", "count(*) as count"},
			Conditions:     where,
			OrderBy:        "out_time asc",
			GroupBy:        "DATE_FORMAT(out_time, '%Y-%m-%d')",
			Page:           page,
			PageSize:       pageSize,
		},
	)

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range orderData {
		outTime := v.OutTime
		returnData = append(returnData, map[string]interface{}{
			"date":        outTime.Format("01月02日"),
			"search_date": []string{outTime.Format("2006-01-02") + " 00:00:00", outTime.Format("2006-01-02") + " 23:59:59"},
			"count":       v.Count,
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		orderStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
			OrderBy:    "out_time asc",
			GroupBy:    "DATE_FORMAT(out_time, '%Y-%m-%d')",
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})
}
